
package com.neegix.organization.bizUnit.interfaces.controller;

import com.neegix.cqrs.command.UniversalCommandBus;
import com.neegix.cqrs.query.UniversalQueryBus;
import com.neegix.organization.bizUnit.application.service.command.CreateBizUnitCommand;
import com.neegix.organization.bizUnit.application.service.command.DeleteBizUnitCommand;
import com.neegix.organization.bizUnit.application.service.command.UpdateBizUnitCommand;
import com.neegix.organization.bizUnit.application.service.command.mapper.BizUnitCommandMapper;
import com.neegix.organization.bizUnit.application.service.query.GetBizUnitDetailQuery;
import com.neegix.organization.bizUnit.application.service.query.GetBizUnitsQuery;
import com.neegix.organization.bizUnit.application.service.query.mapper.BizUnitQueryMapper;
import com.neegix.organization.bizUnit.interfaces.form.NewBizUnitForm;
import com.neegix.organization.bizUnit.interfaces.form.QueryBizUnitForm;
import com.neegix.organization.bizUnit.interfaces.form.UpdateBizUnitForm;
import com.neegix.organization.bizUnit.interfaces.vo.BizUnitVO;
import com.neegix.inferfaces.result.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-12-10 10:58:59
  */

@RestController
@RequestMapping("/api/v1/bizUnit")
public class BizUnitController {

    @Autowired
    private UniversalQueryBus queryBus;

    @Autowired
    private UniversalCommandBus commandBus;

    @PreAuthorize("hasAuthority('organization:bizUnit:add')")
    @PostMapping
    public Result<Void> createBizUnit(@RequestBody @Valid NewBizUnitForm bizUnitForm){
        CreateBizUnitCommand command = BizUnitCommandMapper.INSTANCE.convertCommand(bizUnitForm);
        return Result.success("创建成功", commandBus.execute(command));
    }

    @PreAuthorize("hasAuthority('organization:bizUnit:modify')")
    @PutMapping("/{id}")
    public Result<Void> updateBizUnit(@PathVariable Long id, @RequestBody @Valid UpdateBizUnitForm bizUnitForm){

        UpdateBizUnitCommand command = BizUnitCommandMapper.INSTANCE.convertCommand(bizUnitForm);
        command.setId(id);

        return Result.success("更新成功",commandBus.execute(command));
    }

    @PreAuthorize("hasAuthority('organization:bizUnit:list')")
    @GetMapping
    public Result<List<BizUnitVO>> getBizUnits(@ModelAttribute QueryBizUnitForm bizUnitForm){
        if(bizUnitForm == null) {
            bizUnitForm = new QueryBizUnitForm();
        }
        GetBizUnitsQuery getBizUnitsQuery = BizUnitQueryMapper.INSTANCE.convertQuery(bizUnitForm);
        return Result.success("查询成功",queryBus.execute(getBizUnitsQuery));
    }

    @PreAuthorize("hasAuthority('organization:bizUnit:get')")
    @GetMapping("/{id}")
    public Result<BizUnitVO> getBizUnitById(@PathVariable("id") Long id) {
        return Result.success("获取成功", queryBus.execute(new GetBizUnitDetailQuery(id)));
    }

    @PreAuthorize("hasAuthority('organization:bizUnit:remove')")
    @DeleteMapping
    public Result<Void> removeBizUnit(@RequestBody List<Long> ids){
        return Result.success("删除成功", commandBus.execute(new DeleteBizUnitCommand(ids.get(0))));
    }
}